package com.zfxt.web;

import com.zfxt.biz.model.User;
import com.zfxt.biz.service.IUserService;
import com.zfxt.common.CommonConstants;
import com.zfxt.common.ResultMsg;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2018/9/5.
 */
@Controller
public class LoginController {


    // ----------------------------登录流程------------------------------------

    /**
     * 登录接口
     */
    @ResponseBody
    @RequestMapping("/accounts/signin")
    public String signin(HttpServletRequest req) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String target = req.getParameter("target");
        HttpSession session = req.getSession(true);
            session.setAttribute(CommonConstants.USER_ATTRIBUTE, new User());
            // session.setAttribute(CommonConstants.PLAIN_USER_ATTRIBUTE, user);

        return "success";
//        if (username == null || password == null) {
//            req.setAttribute("target", target);
//            return "/user/accounts/signin";
//        }
//        User user = iUserService.auth(username, password);
//        if (user == null) {
//            return "redirect:/accounts/signin?" + "target=" + target + "&username=" + username + "&"
//                    + ResultMsg.errorMsg("用户名或密码错误").asUrlParams();
//        } else {
//            HttpSession session = req.getSession(true);
//            session.setAttribute(CommonConstants.USER_ATTRIBUTE, user);
//            // session.setAttribute(CommonConstants.PLAIN_USER_ATTRIBUTE, user);
//            return StringUtils.isNoneBlank(target) ? "redirect:" + target : "redirect:/user/home";
     //   }
    }
    /**
     * 登出操作
     *
     * @param request
     * @return
     */
    @RequestMapping("accounts/logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        session.invalidate();
        return "redirect:/index";
    }
}
